package com.zhaogang.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhaogang.dao.GoodsMapper;
import com.zhaogang.dao.SaleGoodsMapper;
import com.zhaogang.dto.GoodsDto;
import com.zhaogang.dto.SaleGoodsDetailDTO;
import com.zhaogang.entity.Goods;
import com.zhaogang.entity.Page;
import com.zhaogang.entity.SaleGoods;

import com.zhaogang.service.GoodsService;
/**
 * 
 * @author xiao.shu
 *2017年4月21日 下午1:20:04
 */
@Service
public class GoodsServiceImpl implements GoodsService{
	 @Autowired GoodsMapper goodsMapper ;
	 @Autowired SaleGoodsMapper saleGoodsMapper;

		/**
		 * 查找特殊商品
		 */
		public List<Goods> selectSpecialProduct() {
			return goodsMapper.selectSpecialProduct();
		}
		/**
		 * 查询所有的
		 */
		
		public List<GoodsDto> queryList(String saleAccount,Page p) {
		
			return goodsMapper.queryList(saleAccount,p.getStartPos(),p.getPageSize());
		}
		
		public Page findpage(String saleAccount,Page page){
			int totalCount=goodsMapper.queryCount(saleAccount);
			Integer pageNow;
			pageNow=page.getPageNow();
			Page p =new Page(totalCount, pageNow);
			p.setTotalCount(totalCount);
			return p;
			}
		
		/**
		 * 新增
		 */
		
		
	    @Transactional
		public void insertObject(GoodsDto goodsDto) {
	    	Goods record = new Goods();
	    	record.setGoodsName(goodsDto.getGoodsName());
	        record.setIcon(goodsDto.getIcon());
	        record.setRam(goodsDto.getRam());
	        record.setRom(goodsDto.getRom());
	        record.setScreenSize(goodsDto.getScreenSize());
	        record.setTimeMarket(goodsDto.getTimeMarket());
	        record.setVersion(goodsDto.getVersion());
	        goodsMapper.insertGoods(record);
	        Integer saleAccountId = goodsMapper.selectId(goodsDto.getSaleAccount());     
			goodsDto.setGoodsId(record.getId());
			goodsDto.setSaleAccountId(saleAccountId);
			saleGoodsMapper.insert(goodsDto);
			
		}
		/**
		 * 修改时查询
		 */
		
		public GoodsDto queryObject(Integer id,String saleAccount) {
		    GoodsDto goodsDto =	new GoodsDto();
			Integer saleAccountId = goodsMapper.selectId(saleAccount);
			goodsDto.setId(id);
			goodsDto.setSaleAccountId(saleAccountId);
			return goodsMapper.queryObject(goodsDto);//传多个参数必须得用对象
		}

		/**
		 * 修改
		 */
		@Transactional
		public void updateObject(GoodsDto goodsDto) {
			Goods goods = new Goods();
			goods.setId(goodsDto.getId());
			goods.setGoodsName(goodsDto.getGoodsName());
			goods.setIcon(goodsDto.getIcon());
			goods.setRam(goodsDto.getRam());
			goods.setRom(goodsDto.getRom());
			goods.setScreenSize(goodsDto.getScreenSize());
			goods.setTimeMarket(goodsDto.getTimeMarket());
			goods.setVersion(goodsDto.getVersion());	
			goodsMapper.updateObject(goods);//修改商品表
			Integer saleAccountId = goodsMapper.selectId(goodsDto.getSaleAccount());
			goodsDto.setSaleAccountId(saleAccountId);
			goodsDto.setSaleGoodsId(goodsDto.getId());
		    Integer saleGoodsId = saleGoodsMapper.selectByPrimaryKey(goodsDto);
			goodsDto.setSaleGoodsId(saleGoodsId);
			saleGoodsMapper.updateByPrimaryKey(goodsDto);//修改关联表
			
			
		}


		/**
		 * 删除
		 */
		public void deleteObject(GoodsDto goodsDto) {
			
			 Integer saleAccountId = goodsMapper.selectId(goodsDto.getSaleAccount());
			 goodsDto.setSaleAccountId(saleAccountId);
			 goodsDto.setSaleGoodsId(goodsDto.getId());
			 Integer saleGoodsId = saleGoodsMapper.selectByPrimaryKey(goodsDto);
			 goodsDto.setSaleGoodsId(saleGoodsId);
			 goodsMapper.deleteObject(goodsDto);}

		
		/**
		 *条件查询
		 */
		public List<Goods> queryByCon(GoodsDto goodsDto) {
			
			return goodsMapper.queryByCon(goodsDto);
		}
		
		
		/*public List<SaleGoodsDetailDTO> queryListForShow() {
			List<SaleGoodsDetailDTO> list = saleGoodsMapper.queryListForShow();
			return list;
		}*/
		
		public List<SaleGoodsDetailDTO> queryListForShow(String keyword) {
			List<SaleGoodsDetailDTO> list = saleGoodsMapper.queryListForShow(keyword);
			return list;
		}
		
		public List<SaleGoodsDetailDTO> specialQuery() {
			return saleGoodsMapper.specialQuery();
			}

		
		public SaleGoods queryBysale_goodsId(int id) {
			 SaleGoods saleGoods = saleGoodsMapper.queryBysale_goodsId(id);
			return saleGoods;
		}
		public Goods queryGoodsByGoodsId(int id) {
			Goods goods =goodsMapper.queryGoodsByGoodsId(id);
			return goods;
		}
		public Integer queryState(String saleAccount) {
			// TODO Auto-generated method stub
			return goodsMapper.queryState(saleAccount);
		}
}
